// Reminder Objects Constructor

/**
 * @param name :
 *            name assigned to the reminder, used to differentiate micro from
 *            macro
 * @param duration :
 *            duration for which the reminder is shown in seconds
 * @param frequency :
 *            frequency with which the reminder is shown in seconds
 */
function Break(name, duration, frequency, notificationAudioFile) {
	var name = name;
	var prevBreakTime = new Date(); 
	
	var soundEnabled = true;
	var duration = duration;
	var frequency = frequency;
	var notificationAudioFile = notificationAudioFile;

	this.getName = function() {
		return name;
	};
	this.getPrevBreakTime = function() {
		return prevBreakTime;
	};
	this.getDuration = function() {
		return duration;
	}; 
	this.getFrequency = function() {
		return frequency;
	};
	this.getNotificationAudioFile = function() {
		return notificationAudioFile;
	};
	this.isSoundEnabled = function() {
		return soundEnabled;
	};

	this.setDuration = function(durationArg) {
		duration = durationArg;
	};
	this.setPrevBreakTime = function(prevBreakTimeArg) {
		prevBreakTime = prevBreakTimeArg;
	};
	this.setFrequency = function(frequencyArg) {
		frequency = frequencyArg;
	};
	this.setNotificationAudioFile = function(notificationAudioFileArg) {
		notificationAudioFile = notificationAudioFileArg;
	};
	this.setSoundEnabled = function(soundEnabledArg) {
		soundEnabled = soundEnabledArg;
	};
};

// Load the default values and store them in localStorage. Check for null so we
// don't overwrite any user defined preferences
Break.prototype.persistValues = function() {
	var breakName = this.getName();

	if (localStorage.getItem(breakName + StorageNames.FREQUENCY) == null) {
		localStorage.setItem(breakName + StorageNames.FREQUENCY, this
				.getFrequency());
	}
	if (localStorage.getItem(breakName + StorageNames.DURATION) == null) {
		localStorage.setItem(breakName + StorageNames.DURATION, this
				.getDuration());
	}
	if (localStorage.getItem(breakName + StorageNames.SOUND_FILE) == null) {
		localStorage.setItem(breakName + StorageNames.SOUND_FILE, this
				.getNotificationAudioFile());
	}
	if (localStorage.getItem(breakName + StorageNames.SOUND_SETTING) == null) {
		localStorage.setItem(breakName + StorageNames.SOUND_SETTING, true);
	}
};

/**
 * @param break_frequency in seconds
 * @param break_duration in seconds
 * @param sound_file
 * @param sound_setting
 */
Break.prototype.storeSettings = function(break_frequency, break_duration, sound_file, sound_setting) {
	var breakName = this.getName();

	if (break_frequency) {
		localStorage.setItem(breakName + StorageNames.FREQUENCY, break_frequency);
	}
	if (break_duration) {
		localStorage.setItem(breakName + StorageNames.DURATION, break_duration);
	}
	if (sound_file) {
		localStorage.setItem(breakName + StorageNames.SOUND_FILE, sound_file);
	}
	if (sound_setting) {
		localStorage.setItem(breakName + StorageNames.SOUND_SETTING, sound_setting);
	}
};

Break.prototype.loadValues = function() {
	var breakName = this.getName();

	if (localStorage.getItem(breakName + StorageNames.FREQUENCY) != null) {
		this.setFrequency(localStorage.getItem(breakName
				+ StorageNames.FREQUENCY)); 
	}
	if (localStorage.getItem(breakName + StorageNames.DURATION) != null) {
		this.setDuration(localStorage.getItem(breakName
				+ StorageNames.DURATION));
	}
	if (localStorage.getItem(breakName + StorageNames.SOUND_FILE) != null) {
		this.setNotificationAudioFile(localStorage.getItem(breakName
				+ StorageNames.SOUND_FILE));
	}
	if (localStorage.getItem(breakName + StorageNames.SOUND_SETTING) != null) {
		this.setSoundEnabled(parseBoolean(localStorage.getItem(breakName
				+ StorageNames.SOUND_SETTING)));
	}
};

Break.prototype.getNextBreakTime = function() {
	var currentTime = new Date();
	var nextBreakTime = new Date();

	nextBreakTime.setTime(currentTime.getTime() + this.getFrequency() * 1000);
	return nextBreakTime;
};

Break.prototype.getNextBreakTimeIncludingDuration = function() {
	var nextBreakTime = this.getNextBreakTime();
	var nextBreakDurationTime = this.getDuration();
	
	return new Date(new Date(nextBreakTime).getTime() + nextBreakDurationTime * 1000);
};